#!/usr/bin/python
#coding:utf-8

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import csvout
import sys

# driver = webdriver.Firefox()
driver = webdriver.PhantomJS()
# driver = webdriver.PhantomJS(executable_path="/home/gtmer/spider/phantomjs-2.1.1-linux-x86_64/bin/phantomjs")
driver.get('http://cnnvd.org.cn/web/wz/tzdym.tag?sign=czsjlf')
time.sleep(5)
#不在同一个frame
driver.switch_to_frame('sjlfframe')
driver.find_element_by_id('cvCname').send_keys(sys.argv[1])
driver.find_element_by_class_name('bd_b2').click()
# time.sleep(10)
# try:
#     element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'lbPagerTitle')))
# finally:
time.sleep(5)
#只能找到element，不能定位到text()[2]
try:
    # //*[@id="queryFromV"]/div/div[1]/ul/li[1]/div[1]/a
    # //*[@id="queryFromV"]/div/div[1]/ul/li[10]/div[1]/a
    res = driver.find_elements_by_xpath('//*[@id="queryFromV"]/div/div[1]/ul/li/div[1]/a')
    for r in res:
        print r.get_attribute('href')

    

except:
    #如果找不到就报错，此时数值为0
    res = ''
else:
    pass
    # num = filter(lambda x: x.isdigit(), res)
finally:
    # outdic = {
    #     'cnnvd_num': num
    # }
    # outli = ['cnnvd_num', num, driver.current_url]
    # csvout.list2csv(outli)
    print(res)
    driver.close()

# if res is None:
#     num = 0
# else:
#     num = filter(lambda x: x.isdigit(), res)
